package demo.Thread;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/10-08-10-19:39
 * @Description：demo.Thread
 */
/*
火车站卖票实例，四个卖票窗口随机卖出50张票
 */
public class BuyTicket {
    public static void main(String[] args) {
        Thread thread1 = new ticket("窗口一");
        Thread thread2 = new ticket("窗口二");
        Thread thread3 = new ticket("窗口三");
        Thread thread4 = new ticket("窗口四");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
class ticket extends Thread{
    public ticket(String name) {
        super(name);
    }

    static int ticket_all = 50;
    static Object obj = new Object();
    @Override
    public void run() {
        while(true) {
            synchronized (obj) {
                if (ticket_all > 0) {
                    System.out.println(this.getName() + "正在卖出第 " + ticket_all + " 张票");
                    ticket_all--;
                }else {
                    break;
                }
            }

        }
    }
}
